package com.sqstudio.exercise.chapter4{
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;

	[SWF(width="800",height="600")]
	
	/**
	 *影片剪辑材质 
	 * @author Nestor
	 * 
	 */	
	public class MovieMaterialExample extends BasicView
	{
		private var plane:Plane;
		public function MovieMaterialExample(viewportWidth:Number=800, viewportHeight:Number=600, scaleToStage:Boolean=true, interactive:Boolean=false, cameraType:String="Target")
		{
			super(viewportWidth, viewportHeight, scaleToStage, interactive, cameraType);
			stage.frameRate = 40;
			init();
			this.startRendering();
			viewport.interactive = true;
		}
		
		private function init():void{
			var materialShape:Sprite = new Sprite();
			materialShape.graphics.beginFill(0x333333);
			materialShape.graphics.drawRect(0,0,300,200);
			materialShape.graphics.endFill();
			
			var txt:TextField = new TextField();
			txt.text = "Hello Pv3d!";
			txt.width = 150;
			txt.setTextFormat(new TextFormat(null,30));
			txt.x = materialShape.width/2 - txt.width/2;
			txt.y = materialShape.height/2- txt.height/2;
			materialShape.addChild(txt);
			addChild(materialShape);
			
			var material:MovieMaterial = new MovieMaterial(materialShape,false,false,true,new Rectangle(0,0,300,200));
			material.smooth = true;
			material.doubleSided = true;
			material.interactive = true;
			
			this.plane = new Plane(material,500,400,10,10);
			scene.addChild(plane);
			
			Sprite(MovieMaterial(plane.material).movie).addEventListener(MouseEvent.CLICK,materialCKHandler);
		}
		
		private function materialCKHandler(e:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
			trace("ok!");
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			plane.localRotationY++;
			super.onRenderTick(event);
		}
	}
}